package com.open.party.cloud.view.home.dangYuanJiaoYu.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.open.party.cloud.R;
import com.open.party.cloud.model.YunKePlayBean;
import com.sinothk.android.utils.XUtils;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import com.sinothk.android.utils.inters.OnSuperListener;

public class LiangDuYunKeTangPlayListAdapter extends RecyclerView.Adapter<LiangDuYunKeTangPlayListAdapter.ViewHolder> {

    // 展示数据
    private ArrayList<YunKePlayBean> mData;
    private Context mContext;
    private OnSuperListener<YunKePlayBean> onItemClickListener;

    public LiangDuYunKeTangPlayListAdapter(Context mContext) {
        this.mContext = mContext;
        this.mData = new ArrayList<>();
    }

    public void setOnSuperListener(OnSuperListener<YunKePlayBean> onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setData(List<YunKePlayBean> data) {
        if (mData == null) {
            mData = new ArrayList<>();
        } else {
            mData.clear();
        }
        if (data == null) {
            data = new ArrayList<>();
        }
        this.mData.addAll(data);
        notifyDataSetChanged();
    }

    public void updateData(List<YunKePlayBean> data) {
        this.mData.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.liang_du_yun_ke_tang_play_list_item, viewGroup, false);
        return new ViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        // 绑定数据
        final YunKePlayBean itemData = mData.get(position);

        holder.titleTv.setText(XUtils.string().getNotNullValue(itemData.getName()));

        if (onItemClickListener != null) {
            holder.rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onClick(position, itemData, "");
                }
            });
        }

        if (itemData.isPlaying()) {
            holder.playStatusIv.setImageResource(R.drawable.icon_play_stop);
        }else{
            holder.playStatusIv.setImageResource(R.drawable.icon_playing);
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        RelativeLayout rootView;
        TextView titleTv;
        ImageView playStatusIv;

        ViewHolder(View itemView) {
            super(itemView);
            rootView = itemView.findViewById(R.id.rootView);

            titleTv = itemView.findViewById(R.id.titleTv);
            playStatusIv = itemView.findViewById(R.id.playStatusIv);
        }
    }
}